package com.yydm.serviceucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yydm.commonutils.JwtUtils;
import com.yydm.commonutils.MD5;
import com.yydm.servicebase.exceptionhandler.LException;
import com.yydm.serviceucenter.entity.Ucenter;
import com.yydm.serviceucenter.entity.vo.RegisterVo;
import com.yydm.serviceucenter.mapper.UcenterMapper;
import com.yydm.serviceucenter.service.UcenterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yydm
 * @since 2022-07-08
 */
@Service
public class UcenterServiceImpl extends ServiceImpl<UcenterMapper, Ucenter> implements UcenterService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //    登录的方法
    @Override
    public String login(Ucenter ucenter) {
//        获取邮箱
        String email = ucenter.getEmail();
//        获取密码
        String password = ucenter.getPassword();

//        邮箱和密码非空判断
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            throw new LException(500, "登录的邮箱或密码为空");
        }

//        判断邮箱是否正确
        QueryWrapper<Ucenter> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        Ucenter ucenter1 = baseMapper.selectOne(wrapper);
//        判断查询对象是否为空
        //没有这个邮箱 登录失败 判断为空
        if (ucenter1 == null) {
            throw new LException(500, "邮箱错误");
        }

//        判断密码
//        因为存储到数据库密码肯定加密
//        把输入的密码加密在和数据库中的密码比较
//        加密方式: MD5
        if (!MD5.encrypt(password).equals(ucenter1.getPassword())) {
            throw new LException(500, "密码错误");
        }

//        判断用户是否禁用
        if (ucenter1.getIsDisabled()) {
            throw new LException(500, "用户被禁用");
        }

//        登录成功
//        生成toKen字符串,使用jwt工具类
        String jwtToKen = JwtUtils.getJwtToKen(ucenter1.getId(), ucenter1.getUsername());

        return jwtToKen;
    }

    //    注册的方法
    @Override
    public void register(RegisterVo registerVo) {
//        用户名
        String username = registerVo.getUsername();
//        密码
        String password = registerVo.getPassword();
//        邮箱
        String email = registerVo.getEmail();
//        验证码
        String code = registerVo.getCode();

        //        邮箱和密码,用户名,验证码非空判断
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(username) || StringUtils.isEmpty(code)) {
            throw new LException(500, "注册失败,为空");
        }

//        判断验证码,
//        获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(email);
        if (!code.equals(redisCode)) {
            throw new LException(500, "注册失败,验证码不正确");
        }

//        判断email是否重复,表里面有重复的邮箱不进行添加
        QueryWrapper<Ucenter> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new LException(500, "邮箱已存在");
        }

//        数据添加到数据库
        Ucenter ucenter = new Ucenter();
        ucenter.setEmail(email);
        ucenter.setPassword(MD5.encrypt(password));
        ucenter.setUsername(username);
        ucenter.setIsDisabled(false);  //用户不禁用
        ucenter.setPicture("https://yydm-automobile-oss.oss-cn-beijing.aliyuncs.com/aodicsr.png");
        baseMapper.insert(ucenter);

    }

    //  统计某一天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.selectRegisterCount(day);
    }

}
